package cn.com.dhcc.turbo.service.registry;
  
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.dao.CrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.AbstractCrudService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.turbo.dao.registry.AdapterInfoDao;
import cn.com.dhcc.turbo.entity.registry.AdapterInfo;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo.SyncOperType;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo.SyncType;
import cn.com.dhcc.turbo.service.registry.sync.SynchronizeHandler;

/**
 * 适配器service
 * 
 * @日期：2014-10-20  
 * @作者：古玉龙
 * @版权所有：东华软件
 * @版本：1.0
 */
@Service
public class AdapterInfoService extends AbstractCrudService<AdapterInfo> { 
	@Autowired
	private AdapterInfoDao dao;
	@Autowired
	SynchronizeHandler synchronizeHandler;
	@Autowired
	LocalConfCallBack localConfCallBack;
	private Logger log = Log.getLogger(LOGTYPE.TURBO);
	
	@Override
	public CrudDao<AdapterInfo> getCrudDao() {
		return dao;
	}


	/**
	 * 获取配置器 分页
	 * 
	 * @param pager
	 * @param condition
	 * @return
	 * @throws ServiceException
	 */ 
	public PagerData<AdapterInfo> getLocalAdapterInfo(Pager pager, WhereCondition condition)
			throws ServiceException {
		try {
			return dao.getLocalAdapterInfo(pager, condition);
		} catch (DaoException e) {
			throw new ServiceException("获取业务域列表失败", e);
		}
	}
	
	/**
	 * 获取本地节点编码
	 * */
	public String localCode() throws ServiceException {
		try {
			return dao.localCode();
		} catch (DaoException e) {
			throw new ServiceException("业务域编码失败", e);
		}
	}
	
	/**
	 * 获取最大编码
	 * */
	public int localMaxCode(String localNodeCode) throws ServiceException {
		try {
			return dao.localMaxCode(localNodeCode);
		} catch (DaoException e) {
			throw new ServiceException("适配器编码失败", e);
		}
	};
	
	
	/**
	 * 获取本地节点明
	 * */
	public String getNoDeName(String nodeCode) throws ServiceException {
		try {
			return dao.getNoDeName(nodeCode);
			
		} catch (DaoException e) {
			throw new ServiceException("适配器编码失败", e);
		}
	};
	
	public List<AdapterInfo> getLocalName() throws ServiceException{
		try {
			return dao.getLocalName();
		} catch(DaoException e){
			throw new ServiceException("本地节点失败",e);
		}
		
	}

	/**
	 * 根据节点编码，系统编码，适配器编码获取对应的适配器信息
	 * @param nodeCode
	 * @param sysCode
	 * @param adapterCode
	 * @return
	 * @throws ServiceException 
	 */
	public AdapterInfo getAdapterInfo(String nodeCode,String adapterCode) throws  ServiceException{
		try {
		   return dao.getAdapterInfo(nodeCode, adapterCode);
		} catch(DaoException e){
			throw new ServiceException("获取适配器信息失败",e);
		}
	}
	/**
	 * 添加并同步
	 * @param entity
	 * @throws ServiceException
	 * @throws NiceException
	 */
	public void insertAdapterAndSync(AdapterInfo entity) throws ServiceException, NiceException {
		super.insert(entity);
		try {
			synchronizeHandler.syncToAllNodesFromLocalNode(SyncType.ADAPTER, false, entity, SyncOperType.INSERT,true);
		} catch (Exception e) {
			throw new NiceException("添加成功，但数据同步失败，请手动同步!");
		}
	}
	/**
	 * 修改并同步
	 * @param entity
	 * @throws ServiceException
	 * @throws NiceException
	 */
	public void updateAdapterAndSync(AdapterInfo entity) throws ServiceException, NiceException {
		super.updateIgnoreNull(entity);
		try {
			AdapterInfo inDbentity = get(entity.getId());
			synchronizeHandler.syncToAllNodesFromLocalNode(SyncType.ADAPTER, false, inDbentity, SyncOperType.UPDATE,true);
		} catch (Exception e) {
			throw new NiceException("添加成功，但数据同步失败，请手动同步!");
		}
		
		try {
			localConfCallBack.onAdapterRegist(entity);
		} catch (Exception e) {
			throw new NiceException("系统注册成功，但为系统分配FTP账号失败!");
		}
	}


	public int insertOrUpdateRemoteNode(AdapterInfo adapterInfo)  throws ServiceException{
		int r = super.insertOrUpdate(adapterInfo);
		return r;
	}


	public List<AdapterInfo> getLocalAdapterList() throws ServiceException{
		try {
			return dao.getLocalAdapterList();
		} catch (DaoException e) {
			throw new ServiceException("获取适配器失败", e);
		}
	}
	/**
	 * 判断本地适配器是否已建立
	 * @return
	 */
	public boolean isLocalAdapterCreate() {
		boolean flag = true;
		try {
			List<AdapterInfo> list = dao.getLocalAdapterList();
			if(list.size() == 0){
				flag = false;
			}
		} catch (Exception e) {
			log.error(e);
		}
		return flag;
	}


	public List<AdapterInfo> getRelNodeAdapterList() throws ServiceException{
		try {
			return dao.getRelNodeAdapterList();
		} catch (DaoException e) {
			throw new ServiceException("获取适配器失败", e);
		}
	}
}
